const parameter = getApp().globalData.parameter
const domainName = getApp().globalData.domainName

const opt = {media_type:'',name:'',checked:''}
const q = {q:'',opt_list:[opt,opt,opt]}
// pages/sysConfig/config_gs/add_ti/add_ti.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    lis:[],
    windowWidth:wx.getSystemInfoSync().windowWidth,
    info:{
      type_id:'',
      title:'',
      analysis:'',
      pic:'', 
      q_list:[q,q,q,q,q],
    }
  },

  // 提交
  submit(){
    wx.showModal({
      content: '确定提交吗？',
      title: '提示',
      success: res => {
        if(res.confirm){
          if(this.data.tempFilePath){ // 如果有【tempFilePath】，则是由图片提交
            this.submit_with_pic()
          }
        }
      },
    })
  },


  // 文字&图片同时提交
  submit_with_pic(){
    wx.showLoading()
    wx.uploadFile({
      filePath: this.data.info.pic,
      name: 'files',
      url: domainName + '/gs_add_ti_submit',
      formData: {
        info:JSON.stringify(this.data.info)
      },
      header : {'content-type':'application/x-www-form-urlencoded'},
      success: res => {
        wx.hideLoading()
        if(JSON.parse(res.data).row_id){
          wx.showToast({ title: '提交完成！'})
          wx.navigateBack()
        }
      },
    })
  },





  // 选择题目类型
  set_type(e){
    let index = e.detail.value
    let info = this.data.info
    info.type_id = this.data.lis[index].id
    this.setData({info,})
  },

  // 删除选项
  del_opt(e){
    let index1 = e.currentTarget.dataset.index1
    let index2 = e.currentTarget.dataset.index2
    let info = this.data.info
    info.q_list[index1].opt_list.splice(index2,1)
    this.setData({info:info})
    wx.showToast({title: '删除完成'})
  },

  // 增加选项
  add_opt(e){
    let index1 = e.currentTarget.dataset.index1
    let info = this.data.info
    var opt = {media_type:'',name:'',checked:''}
    info.q_list[index1].opt_list.push(opt)
    this.setData({info:info})
  },

  // 增加问题
  add_q(){
    let info = this.data.info
    var opt = {media_type:'',name:'',checked:''}
    var q = {q:'',opt_list:[opt,opt,opt]}
    info.q_list.push(q)
    this.setData({info:info})
  },
  // 删除问题
  del_q(e){
    let index1 = e.currentTarget.dataset.index1
    let info = this.data.info
    info.q_list.splice(index1,1)
    this.setData({info:info})
    wx.showToast({title: '删除完成'})
  },

  // 挑选图片
  chooseimg(){
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sizeType: ['compressed'],
      sourceType: ['album'],
      success: res => {
        let tempFile = res.tempFiles[0]
        if(tempFile.size>100000){
          wx.showModal({
            content: '上传文件不宜大于100KB',
            showCancel: false,
            title: '提示',
          })
        }else{
          let info = this.data.info
          let tempFilePath = tempFile.tempFilePath
          info.pic = tempFilePath
          this.setData({info,tempFilePath})
        }

      },
    })
  },

  // 输入题干
  input_title(e){
    let value = e.detail.value
    let info = this.data.info
    info.title = value
    this.setData({info:info})
  },

  // 输入解题思路
  input_analysis(e){
    let value = e.detail.value
    let info = this.data.info
    info.analysis = value
    this.setData({info:info})
  },

  // 输入小题题目
  input_q(e){
    let index1 = e.currentTarget.dataset.index1
    let value = e.detail.value
    let info = this.data.info
    info.q_list[index1].q = value
    this.setData({info:info})
  },
  

  // 输入选项
  input_opt(e){
    let index1 = e.currentTarget.dataset.index1
    let index2 = e.currentTarget.dataset.index2
    let value = e.detail.value
    let info = this.data.info
    info.q_list[index1].opt_list[index2].name = value
    info.q_list[index1].opt_list[index2].media_type = 1 //1-文字
    this.setData({info:info})
  },

  // 设置答案
  set_answer(e){
    let index1 = e.currentTarget.dataset.index1
    let value = e.detail.value
    let info = this.data.info
    let opt_list = info.q_list[index1].opt_list
    for(var i=0; i<opt_list.length;i++){
      if(i==value){
        var checked=1
      }else{
        var checked=''
      }
      opt_list[i].checked = checked
    }
    this.setData({info:info})
  },

  page_init(){
    wx.request({
      url: domainName + '/gs_add_ti_init',
      data: {},...parameter,
      success: res => {
        this.setData({
          lis:res.data.lis
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.page_init()
  },

})